package com.axiang.module_home.viewmodel;

import android.app.Application;
import androidx.annotation.NonNull;

import com.axiang.lib_api.response.BaseResponse;
import com.axiang.lib_common.mvvm.viewmodel.BaseRefreshViewModel;
import com.axiang.lib_common.network.RefreshObserver;
import com.axiang.module_home.BR;
import com.axiang.module_home.R;
import com.axiang.module_home.bean.HomeMessageBean;
import com.axiang.module_home.model.HomeModel;

import java.util.List;

public class HomeViewModel extends BaseRefreshViewModel<HomeMessageBean.DatasBean, HomeModel> {
    private int pageNo;

    public HomeViewModel(@NonNull Application application, HomeModel model) {
        super(application, model);
    }

    @Override
    public int onBindItemVariableId() {
        return BR.viewModel;
    }

    @Override
    public int onBindItemLayout() {
        return R.layout.item_homelist;
    }

    @Override
    public void refreshData() {
        pageNo = 1;

        mModel.getMessageList(pageNo).subscribe(new RefreshObserver<HomeMessageBean, HomeViewModel>(this) {
            @Override
            public void onSuccess(HomeMessageBean homeMessageBeans) {
                List<HomeMessageBean.DatasBean> datasBeans = homeMessageBeans.getDatas();
                if (datasBeans != null && datasBeans.size() > 0) {
                    mList.clear();
                    mList.addAll(datasBeans);
                } else {
                    postShowNoDataViewEvent(true);
                }
            }
        });

    }

    @Override
    public void loadMore() {
        pageNo++;

        mModel.getMessageList(pageNo).subscribe(new RefreshObserver<HomeMessageBean, HomeViewModel>(this) {
            @Override
            public void onSuccess(HomeMessageBean homeMessageBeans) {

                List<HomeMessageBean.DatasBean> datasBeans = homeMessageBeans.getDatas();
                if (datasBeans != null && datasBeans.size() > 0) {
                    mList.addAll(datasBeans);
                }
            }
        });
    }
}
